package club.terroir.client.producteur;



import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;

import club.terroir.client.Style;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ContactUpdateGUI extends VerticalPanel {
	
	public static ContactUpdateGUI instance = new ContactUpdateGUI();
	Button buttonUpdateContact = new Button("Metttre à jour");
	Button buttonClear= new Button("Clear Contact");
	private Label labelTitreProduitSelected= new Label("");
	SuggestBox suggestBoxId_ = new SuggestBox( new MultiWordSuggestOracle());
	SuggestBox suggestBoxName_ = new SuggestBox( new MultiWordSuggestOracle());
	SuggestBox suggestBoxFirstName = new SuggestBox( new MultiWordSuggestOracle());
	SuggestBox suggestBoxTelephone = new SuggestBox( new MultiWordSuggestOracle());
	SuggestBox suggestBoxEmail = new SuggestBox( new MultiWordSuggestOracle());
	SuggestBox suggestBoxAddress1 = new SuggestBox( new MultiWordSuggestOracle());
	SuggestBox suggestBoxAddress2 = new SuggestBox(new MultiWordSuggestOracle());
	SuggestBox suggestBoxVille = new SuggestBox( new MultiWordSuggestOracle());
	SuggestBox suggestBoxCodePostal = new SuggestBox( new MultiWordSuggestOracle());
	Label labelTitre = new Label("Contact :");
	//Label labelId = new Label("xx");
	Grid grid ;
	private Contact contactInfo;
	int debug;
	Collection<Contact> listContactSuggest = new HashSet<Contact>();
	private HashMap<SuggestBox, SuggestBox> mapSuggestNextFocus = new HashMap<SuggestBox, SuggestBox>();
	private HashMap<SuggestBox, Collection<String>> mapSuggestListSuggestion = new HashMap<SuggestBox, Collection<String>>();
	
	public ContactUpdateGUI() {
		super();
		labelTitre.setStyleName(Style.Titre);
		initMapNextFocus();
		initSuggestList();
		initSuggestBoxFirst(this.suggestBoxId_);
		initSuggestBoxFirst(this.suggestBoxName_);
		initSuggestBoxFirst(this.suggestBoxFirstName);
		initSuggestBoxFirst(this.suggestBoxTelephone);
		initSuggestBoxFirst(this.suggestBoxEmail);
		initSuggestBoxFirst(this.suggestBoxAddress1);
		initSuggestBoxFirst(this.suggestBoxAddress2);
		initSuggestBoxFirst(this.suggestBoxCodePostal);
		initSuggestBoxFirst(this.suggestBoxVille);
		
		this.add(labelTitre);
		grid = new Grid(11, 2);
		//grid.setBorderWidth(1);
		addToGrid(0,"Id",suggestBoxId_);
		addToGrid(1,"Name",suggestBoxName_);
		addToGrid(2,"Prenom",suggestBoxFirstName);
		addToGrid(3,"Téléphone",suggestBoxTelephone);
		addToGrid(4,"E-mail",suggestBoxEmail);
		addToGrid(5,"Adresse 1",suggestBoxAddress1);
		addToGrid(6,"Adresse 2",suggestBoxAddress2);
		addToGrid(7,"Code Postal",suggestBoxCodePostal);
		addToGrid(8,"Ville",suggestBoxVille);
		addToGrid(9,"--",new Label(" --"));
		this.add(grid);
		this.add(labelTitreProduitSelected);
		
		Panel panelButtons = new HorizontalPanel();
		panelButtons.setStyleName(Style.buttonMenu);
		buttonUpdateContact.setStyleName(Style.buttonMenu);
		panelButtons.add(buttonUpdateContact);
		panelButtons.add(buttonClear);
		this.add(panelButtons);
		
		this.buttonClear.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				clearField(new Contact());
			}
		});
		
		this.clearField(null);
	}
	
	
	private void initMapNextFocus() {
		this.mapSuggestNextFocus.put(this.suggestBoxId_, this.suggestBoxName_);
		this.mapSuggestNextFocus.put(this.suggestBoxName_, this.suggestBoxFirstName);
		this.mapSuggestNextFocus.put(this.suggestBoxFirstName, this.suggestBoxTelephone);
		this.mapSuggestNextFocus.put(this.suggestBoxTelephone, this.suggestBoxEmail);
		this.mapSuggestNextFocus.put(this.suggestBoxEmail, this.suggestBoxAddress1);
		this.mapSuggestNextFocus.put(this.suggestBoxAddress1, this.suggestBoxAddress2);
		this.mapSuggestNextFocus.put(this.suggestBoxAddress2, this.suggestBoxCodePostal);
		this.mapSuggestNextFocus.put(this.suggestBoxCodePostal, this.suggestBoxVille);
		this.mapSuggestNextFocus.put(this.suggestBoxVille, this.suggestBoxId_);
		
	}
	private void initSuggestBoxFirst(final SuggestBox sg){
		sg.addSelectionHandler(new SelectionHandler<Suggestion>() {
			@Override
			public void onSelection(SelectionEvent<Suggestion> event) {
				try {
					updateSuggestBoxFromName(sg);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		sg.getTextBox().addFocusHandler(new FocusHandler() {			
			@Override
			public void onFocus(FocusEvent event) {				
				try {
					sg.showSuggestionList();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	private void initSuggestList() {
		String id  =  this.suggestBoxName_.getText();
		String name = this.suggestBoxName_.getText();
		String firstName = this.suggestBoxFirstName.getText();
		String telephone = this.suggestBoxTelephone.getText();
		String email = this.suggestBoxEmail.getText();
		String address1 = this.suggestBoxAddress1.getText();
		String address2 = this.suggestBoxAddress2.getText();
		String codePostal = this.suggestBoxCodePostal.getText();
		String ville = this.suggestBoxVille.getText();
		Contact contactRef = new Contact(id, name, firstName, telephone, email, address1, address2, codePostal, ville);
		this.listContactSuggest = ContactFactory.instance.getSuggestContacts(contactRef);
		this.iniSuggestBox(this.suggestBoxId_,ContactFactory.instance.getSuggestListId(this.listContactSuggest) );
		this.iniSuggestBox(this.suggestBoxName_,ContactFactory.instance.getSuggestListName(this.listContactSuggest) );
		this.iniSuggestBox(this.suggestBoxFirstName,ContactFactory.instance.getSuggestListFistName(this.listContactSuggest)  );
		this.iniSuggestBox(this.suggestBoxTelephone,ContactFactory.instance.getSuggestListTelephone(this.listContactSuggest)  );
		this.iniSuggestBox(this.suggestBoxEmail,ContactFactory.instance.getSuggestListCodeEmail(this.listContactSuggest)  );
		this.iniSuggestBox(this.suggestBoxAddress1,ContactFactory.instance.getSuggestListAddress1(this.listContactSuggest)  );
		this.iniSuggestBox(this.suggestBoxAddress2,ContactFactory.instance.getSuggestListAddress2(this.listContactSuggest)  );
		this.iniSuggestBox(this.suggestBoxCodePostal,ContactFactory.instance.getSuggestListCodePostal(this.listContactSuggest) );
		this.iniSuggestBox(this.suggestBoxVille,ContactFactory.instance.getSuggestListVille(this.listContactSuggest) );
		//this.textBoxAchat.setText("Debug:    "+this.listContactSuggest.size()+" names: "+ContactFactory.instance.getSuggestListName(this.listContactSuggest).size()+"  xxxx "+new Date());
			
	}
	
	private void iniSuggestBox(SuggestBox sb, Collection<String> collect){
		mapSuggestListSuggestion.put(sb, collect);
		MultiWordSuggestOracle oracleNames = (MultiWordSuggestOracle) sb.getSuggestOracle();
		oracleNames.clear();
		oracleNames.addAll(collect);
		oracleNames.setDefaultSuggestionsFromText(collect);
	}
	
	private void updateSuggestBoxFromName(SuggestBox sg ){
		this.initSuggestList();
		
		SuggestBox nextFocus = 	mapSuggestNextFocus.get(sg);
		int nbSuggestions = getNbSuggestions(nextFocus);
		int nbIterartion =0;
		while((nbSuggestions == 1) && (nbIterartion < mapSuggestNextFocus.size())){			
			nbIterartion++;
			Collection<String> collectSuggestions = mapSuggestListSuggestion.get(nextFocus);
			nextFocus.setText(collectSuggestions.iterator().next());
			nextFocus = mapSuggestNextFocus.get(nextFocus);
			nbSuggestions = getNbSuggestions(nextFocus);
		}
		if (nbIterartion <  mapSuggestNextFocus.size()){
			setFocusAndSuggestList(nextFocus);
		}
		
	}
	
	private int getNbSuggestions(SuggestBox sg) {
		Collection<String> listSuggest = this.mapSuggestListSuggestion.get(sg);
		if (listSuggest==null)	{
			return 0;
		}
		return listSuggest.size();
	}
	
	private void setFocusAndSuggestList(SuggestBox suggestBox) {
		suggestBox.setFocus(true);
		suggestBox.showSuggestionList();
	}
	private void addToGrid(int row ,String s, Widget textBox) {
		 grid.setText(row, 0, s);
		 grid.setWidget(row, 1, textBox);
	}

	public Contact getContactInfo() {
		return contactInfo;
	}

	public void setContactInfo(Contact contactInfo) {
		this.contactInfo = contactInfo;
		if (contactInfo == null){
			this.contactInfo = new Contact();
		}		
		clearField(contactInfo);
	}
	
	

	private void initLabels_(){
		labelTitre.setText("Contact :  "+this.contactInfo.name);
		this.suggestBoxId_.setText(this.contactInfo.getId());
		this.suggestBoxName_.setText(this.contactInfo.name);
		this.suggestBoxFirstName.setText(this.contactInfo.firstName);
		this.suggestBoxTelephone.setText(""+this.contactInfo.telephone);
		this.suggestBoxEmail.setText(this.contactInfo.email);
		this.suggestBoxAddress1.setText(this.contactInfo.address1);
		this.suggestBoxAddress2.setText(this.contactInfo.address1);
		this.suggestBoxCodePostal.setText(this.contactInfo.codePostal);
		this.suggestBoxVille.setText(this.contactInfo.ville);
	}
	
	private void clearField(Contact c) {		
		this.contactInfo = c;
		if (this.contactInfo == null){
			this.contactInfo = new Contact();
		}
		this.initLabels_();
		this.initSuggestList();
		
	}
	

}
